package com.mzxy.c1625163415;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mzxy.c1625163415.adapter.NewestRecyclerViewAdapter;
import com.mzxy.c1625163415.model.Stories;
import com.mzxy.c1625163415.model.TopStories;
import com.mzxy.c1625163415.utils.GlideImageLoader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import org.apache.commons.lang3.StringUtils;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 主页面
 * @Version: 1.0
 */
public class MainActivity extends Activity {

    public static final String URL = "https://news-at.zhihu.com/api/4/news/latest";
    public List<String> listUrlTop = new ArrayList<>();
    List<TopStories> listTop = new ArrayList<>();
    List<Stories> list = new ArrayList<>();
    List<String> Titles = new ArrayList<>();
    private Banner banner;
    private TextView tv_title;
    LinearLayoutManager mPerfectCourse = null;
    RefreshLayout likerefreshLayout;
    RecyclerView recyclerView;
    public NewestRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        banner = findViewById(R.id.banner);
        likerefreshLayout = findViewById(R.id.likerefreshLayout);
        recyclerView = findViewById(R.id.likerecyclerView);
        //关闭自动加载功能
        likerefreshLayout.setEnableAutoLoadMore(false);
        mPerfectCourse = new LinearLayoutManager(this);
        mPerfectCourse.setOrientation(LinearLayoutManager.VERTICAL);
        if (adapter == null) {
            adapter = new NewestRecyclerViewAdapter(this, this);
        }
        recyclerView.setLayoutManager(mPerfectCourse);
        recyclerView.setAdapter(adapter);


        network();
        initlistner();
    }

    private void network() {
        // 异步网络请求
        RequestParams params = new RequestParams(URL);
        x.http().get(params, new org.xutils.common.Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = JSON.parseObject(result);
                    String top_stories = jsonObject.getString("top_stories");
                    String stories = jsonObject.getString("stories");
                    if (StringUtils.isBlank(top_stories) || StringUtils.isBlank(stories)) {
                        Toast.makeText(MainActivity.this, "接口异常", Toast.LENGTH_SHORT).show();
                    }
                    listTop = JSONObject.parseArray(top_stories, TopStories.class);
                    list = JSONObject.parseArray(stories, Stories.class);
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "连接异常", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                for (int i = 0; i < listTop.size(); i++) {
                    TopStories top = listTop.get(i);
                    listUrlTop.add(top.getImage());
                    Titles.add(top.getTitle());
                }
                banner.update(listUrlTop, Titles);
            }
        });
        banner.setImageLoader(new GlideImageLoader());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    private void initlistner() {
        //触发自动刷新
        likerefreshLayout.autoRefresh();
        likerefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                likerefreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        RequestParams params = new RequestParams(URL);
                        x.http().get(params, new org.xutils.common.Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                try {
                                    JSONObject jsonObject = JSON.parseObject(result);
                                    String top_stories = jsonObject.getString("top_stories");
                                    String stories = jsonObject.getString("stories");
                                    if (StringUtils.isBlank(top_stories) || StringUtils.isBlank(stories)) {
                                        Toast.makeText(MainActivity.this, "接口异常", Toast.LENGTH_SHORT).show();
                                    }
                                    listTop = JSONObject.parseArray(top_stories, TopStories.class);
                                    list = JSONObject.parseArray(stories, Stories.class);
                                    adapter.refresh(list, true);
                                    likerefreshLayout.finishRefresh();
                                } catch (Exception e) {
                                    Toast.makeText(MainActivity.this, "连接异常", Toast.LENGTH_SHORT).show();
                                    likerefreshLayout.finishRefresh();
                                    likerefreshLayout.resetNoMoreData();
                                }

                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                                likerefreshLayout.finishRefresh();
                                likerefreshLayout.resetNoMoreData();
                            }

                            @Override
                            public void onCancelled(CancelledException cex) {


                            }

                            @Override
                            public void onFinished() {

                            }
                        });

                    }
                }, 100);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        banner.startAutoPlay();
    }

    @Override
    protected void onPause() {
        super.onPause();
        banner.stopAutoPlay();
    }
}
